import { IText, ITextCharacter } from "./IText.js";

export class IButtonIcon extends ITextCharacter {
    constructor() {
        super(true);
        this.SetTagName("IButtonIcon");
        this.SetFontFamily("iconfont");
        this.SetPaddingLeft(4);
        this.SetPaddingRight(4);
    }
}

export class IButton extends IText {
    constructor() {
        super();
        this.SetTagName("IButton");
        this.SetStyleJustifyContent("center");
        this.SetStyleWidth(this.GetThemeInlineWidth());
        this.SetStyleHeight(this.GetThemeInlineHeight());
        this.SetBorder(this.GetThemeBorderWidth(), this.GetThemeBorderStyle(), this.GetThemeBorderColor());
        this.SetButtonBorderSize(this.GetThemeBorderWidth());
        this.SetButtonBorderColor(this.GetThemeBorderColor());
        this.SetFontColor(this.GetThemeSecondaryTextColor());
        this.SetBackgroundColor(this.GetThemeBackgroundColor());

        this.SetHoverFontColor(this.GetThemePrimaryTextColor());
        this.SetHoverBackgroundColor(this.GetThemeHoverBackgroundColor());
        this.SetLastFontColor(this.GetFontColor());
        this.SetLastBackgroundColor(this.GetBackgroundColor());
        this.SetStyleDisplay("inline-flex");

        this.m_type = "default";
        this.m_shape = "default";
        this.m_icon = new IButtonIcon();
        this.m_icon_name = "";
        this.m_icon_style = "";
        this.m_icon_position = "left";
    }

    SetHoverFontColor(hover_font_color) { this.m_hover_font_color = hover_font_color; }
    GetHoverFontColor() { return this.m_hover_font_color; }
    SetHoverBackgroundColor(hover_background_color) { this.m_hover_background_color = hover_background_color; }
    GetHoverBackgroundColor() { return this.m_hover_background_color; }
    SetLastFontColor(last_font_color) { this.m_last_font_color = last_font_color; }
    GetLastFontColor() { return this.m_last_font_color; }
    SetLastBackgroundColor(last_background_color) { this.m_last_background_color = last_background_color; }
    GetLastBackgroundColor() { return this.m_last_background_color; }
    SetButtonBorderSize(border_size) { this.m_button_border_size = border_size; }
    GetButtonBorderSize() { return this.m_button_border_size; }
    SetButtonBorderColor(border_color) { this.m_button_border_color = border_color; }
    GetButtonBorderColor() { return this.m_button_border_color; }

    UpdateAttribute() {
        super.UpdateAttribute();
        const type = this.GetStringAttributeValue("type", this.m_type);
        const shape = this.GetStringAttributeValue("shape", this.m_shape);
        const icon_name = this.GetStringAttributeValue("icon", this.m_icon_name);
        const icon_position = this.GetStringAttributeValue("icon-position", this.m_icon_position);
        const icon_style = this.GetStringAttributeValue("icon-style", this.m_icon_style);
        let layout_changed = false;
        if (type !== this.m_type) {
            this.m_type = type;
            layout_changed = true;
        }
        if (shape !== this.m_shape) {
            this.m_shape = shape;
            layout_changed = true;
        }
        if (icon_name !== this.m_icon_name) {
            this.m_icon_name = icon_name;
            layout_changed = true;
            this.m_icon.SetValue(icon_name);
        }
        if (icon_position !== this.m_icon_position) {
            this.m_icon_position = icon_position;
            layout_changed = true;
        }

        if (icon_style !== this.m_icon_style) {
            this.m_icon.InsertAttribute("style", icon_style);
            this.m_icon_style = icon_style;
            layout_changed = true;
        }
        if (this.m_shape === "plain") {
            if (this.m_type === "primary") {
                this.SetButtonBorderColor(this.GetThemeBasePrimaryColor());
                this.SetBackgroundColor(this.GetThemeExtraLightPrimaryColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkPrimaryColor());
                this.SetFontColor(this.GetThemePrimaryColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            } else if (this.m_type === "success") {
                this.SetButtonBorderColor(this.GetThemeBaseSuccessColor());
                this.SetBackgroundColor(this.GetThemeExtraLightSuccessColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkSuccessColor());
                this.SetFontColor(this.GetThemeSuccessColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            } else if (this.m_type === "warning") {
                this.SetButtonBorderColor(this.GetThemeBaseWarningColor());
                this.SetBackgroundColor(this.GetThemeExtraLightWarningColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkWarningColor());
                this.SetFontColor(this.GetThemeWarningColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            } else if (this.m_type === "danger") {
                this.SetButtonBorderColor(this.GetThemeBaseDangerColor());
                this.SetBackgroundColor(this.GetThemeExtraLightDangerColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkDangerColor());
                this.SetFontColor(this.GetThemeDangerColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            } else if (this.m_type === "info") {
                this.SetButtonBorderColor(this.GetThemeBaseInfoColor());
                this.SetBackgroundColor(this.GetThemeExtraLightInfoColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkInfoColor());
                this.SetFontColor(this.GetThemeInfoColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            } else {
                this.SetButtonBorderColor(this.GetThemeBorderColor());
                this.SetBackgroundColor(this.GetThemeBackgroundColor());
                this.SetHoverBackgroundColor(this.GetThemeHoverBackgroundColor());
                this.SetFontColor(this.GetThemeSecondaryTextColor());
                this.SetHoverFontColor(this.GetThemePrimaryTextColor());
                this.SetBorder(this.GetButtonBorderSize(), this.GetButtonBorderColor());
            }
        } else {
            if (this.m_type === "primary") {
                this.SetBackgroundColor(this.GetThemePrimaryColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkPrimaryColor());
                this.SetFontColor(this.GetThemeBasicWhiteColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorderWidth(0);
            } else if (this.m_type === "success") {
                this.SetBackgroundColor(this.GetThemeSuccessColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkSuccessColor());
                this.SetFontColor(this.GetThemeBasicWhiteColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorderWidth(0);
            } else if (this.m_type === "warning") {
                this.SetBackgroundColor(this.GetThemeWarningColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkWarningColor());
                this.SetFontColor(this.GetThemeBasicWhiteColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorderWidth(0);
            } else if (this.m_type === "danger") {
                this.SetBackgroundColor(this.GetThemeDangerColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkDangerColor());
                this.SetFontColor(this.GetThemeBasicWhiteColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorderWidth(0);
            } else if (this.m_type === "info") {
                this.SetBackgroundColor(this.GetThemeInfoColor());
                this.SetHoverBackgroundColor(this.GetThemeDarkInfoColor());
                this.SetFontColor(this.GetThemeBasicWhiteColor());
                this.SetHoverFontColor(this.GetThemeBasicWhiteColor());
                this.SetBorderWidth(0);
            } else {
                this.SetButtonBorderColor(this.GetThemeBorderColor());
                this.SetBackgroundColor(this.GetThemeBackgroundColor());
                this.SetHoverBackgroundColor(this.GetThemeHoverBackgroundColor());
                this.SetFontColor(this.GetThemeSecondaryTextColor());
                this.SetHoverFontColor(this.GetThemePrimaryTextColor());
                this.SetBorderWidth(this.GetButtonBorderSize());
            }
        }

        this.DeleteChildren(this.m_icon);
        this.InsertChildren(this.m_icon, this.m_icon_position == "left" ? (this.GetChildrens()[0]) : null);
        this.SetLayoutChanged(this.IsLayoutChanged() || layout_changed);
    }

    SetText(text) {
        super.SetText(text);
        this.InsertChildren(this.m_icon, this.m_icon_position == "left" ? (this.GetChildrens()[0]) : null);
    }

    OnRenderBackground(painter) {
        const border_size = this.GetButtonBorderSize();
        const border_color = this.GetButtonBorderColor();
        const background_color = this.IsDisabled() ? this.GetThemePageBackgroundColor() : this.GetBackgroundColor();
        const x = this.GetX();
        const y = this.GetY();
        const width = this.GetWidth();
        const height = this.GetHeight();
        if (this.m_shape == "round") {
            const radius = height / 2;
            if (border_size > 0) painter.FillRoundRectangle(x, y, width, height, radius, border_color);
            painter.FillRoundRectangle(x + border_size, y + border_size, width - 2 * border_size, height - 2 * border_size, radius - border_size, background_color);
        }
        else if (this.m_shape == "circle") {
            const radius = (height < width ? height : width) / 2;
            const circle_x = x + width / 2;
            const circle_y = y + height / 2;
            if (border_size > 0) painter.FillArc(circle_x, circle_y, radius, 0, 360, border_color);
            painter.FillArc(circle_x, circle_y, radius - border_size, 0, 360, background_color);
        }
        else {
            painter.FillRectangle(x, y, width, height, background_color);
        }
    }

    OnRenderBorder(painter) {
        if (this.m_shape == "round" || this.m_shape == "circle") return;
        super.OnRenderBorder(painter);
    }

    OnMouseEnter() {
        super.OnMouseEnter();
        // 备份旧样式
        this.SetLastFontColor(this.GetFontColor());
        this.SetLastBackgroundColor(this.GetBackgroundColor());
        // 设置悬浮样式
        this.SetFontColor(this.GetHoverFontColor());
        this.SetBackgroundColor(this.GetHoverBackgroundColor());
        // 刷新元素
        this.Refresh();
    }

    OnMouseLeave() {
        super.OnMouseLeave();
        // 恢复旧样式
        this.SetFontColor(this.GetLastFontColor());
        this.SetBackgroundColor(this.GetLastBackgroundColor());
        // 刷新元素
        this.Refresh();
    }
}

export class Button extends IButton {
    constructor() {
        super();
        this.SetTagName("Button");
    }
}